﻿/**
FlexFlow Reception Library version 1.0b
**/
//执行指定操作
function doOperation(opt) {
    if (!opt.slient) {
        $.blockUI({
            message: $("#loader").clone()
        });
    }
    $.post(opt.url, opt.data)
        .done(function (d) {
            if (!opt.slient) {
                $.unblockUI();
            }
            switch (d.ResultCode) {
                case 200:
                    if (opt.success != null) {
                        opt.success(d);
                    }
                    break;
                case 400:
                    if (d.Tag == "Ulogin") {
                        needLogin();
                    }
                    break;
                default:
                    if (opt.showResult == null || opt.showResult) {
                        bootbox.alert(d.Message + "[" + d.ResultCode + "]" + "->" + d.SourceMessage);
                    }
                    if (opt.fail != null) {
                        opt.fail(d);
                    }
                    break;
            }
        })
        .fail(function (d) {
            if (!opt.slient) {
                $.unblockUI();
            }
            if (opt.error != null) {
                console.info(d);
                bootbox.alert("执行请求失败：" + d.statusText + "[" + d.status + "]");
                opt.error();
            }
        });
}
//跳转到登录界面
function needLogin() {
    bootbox.alert("用户登录超时或者未登录用户，请重新登录！", function () {
        window.location = "/UserAccount/Login";
    })
}
//登出
function logout() {
    doOperation({
        'url': '/userapi/logout',
        'success': function (d) {
            bootbox.alert("操作成功！", function () {
                window.location = "/UserAccount/Login";
            });
        }
    })
}
//函数：获取尺寸
function findDimensions() {
    var winWidth = 0;
    var winHeight = 0;
    //获取窗口宽度
    if (window.innerWidth)
        winWidth = window.innerWidth;
    else if ((document.body) && (document.body.clientWidth))
        winWidth = document.body.clientWidth;
    //获取窗口高度
    if (window.innerHeight)
        winHeight = window.innerHeight;
    else if ((document.body) && (document.body.clientHeight))
        winHeight = document.body.clientHeight;
    //通过深入Document内部对body进行检测，获取窗口大小

    if (document.documentElement && document.documentElement.clientHeight &&
        document.documentElement.clientWidth) {
        winHeight = document.documentElement.clientHeight;
        winWidth = document.documentElement.clientWidth;
    }
    return {
        'winWidth': winWidth,
        'winHeight': winHeight
    };
}
//表单验证选项
function buildOpt(submitHandler, invalidHandler) {
    var valOpt = {
        errorElement: 'div',
        errorClass: 'help-block',
        focusInvalid: false,
        rules: {
            email: {
                required: true,
                email: true
            },
            pwd: {
                required: true,
                minlength: 5
            },
            repwd: {
                required: true,
                minlength: 5,
                equalTo: "#pwd"
            },
            account: {
                required: true,
                minlength: 4,
            },
            name: {
                required: true,
                minlength: 2,
            },
            acc: {
                required: true,
                minlength: 4,
            },
            agree: 'required'
        },

        messages: {
            acc: {
                required: "请输入用户名",
                minlength: "用户名至少为4位"
            },
            email: {
                required: "请输入Email",
                email: "请输入一个有效的Email"
            },
            pwd: {
                required: "请输入密码",
                minlength: "密码至少为6位"
            },
            repwd: {
                required: "请输入密码",
                equalTo: "两次密码输入不匹配！"
            },
            agree: "请同意用户协议"
        },

        invalidHandler: function (event, validator) { //display error alert on form submit
            $('.alert-danger', $('.login-form')).show();
        },

        highlight: function (e) {
            $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },

        success: function (e) {
            $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
            $(e).remove();
        },

        errorPlacement: function (error, element) {
            if (element.is(':checkbox') || element.is(':radio')) {
                var controls = element.closest('div[class*="clearfix"]');
                if (controls.find(':checkbox,:radio').length > 1) controls.append(error);
                else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
            }
            else if (element.is('.select2')) {
                error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
            }
            else if (element.is('.chosen-select')) {
                error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
            }
            else error.insertAfter(element.parent());
        },

        submitHandler: submitHandler,
        invalidHandler: invalidHandler
    }
    return valOpt;
};
//取得url参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}
//开始流程
function startProcess(procId) {
    $.ajax({
        type: 'GET',
        url: "/FlexFlow/StartProcess?procId=" + procId,
        success: function (data) {
            bootbox.dialog({
                message: data,
                title: "发起流程"
            });
        }
    });
}
//打开新的浏览器窗口
function openNewWindow(url, title) {
    if (!window.open(url, title)) {
        bootbox.confirm("打开新窗口的请求被浏览器阻止，请检查您的浏览器设置，要尝试在当前窗口打开么？", function (result) {
            if (result) {
                navgateUrl(url);
            }
        })
    }
}